string1 = "Lua"
print("\"字符串 1 是\"",string1)
string2 = 'wdxtub.com'
print("字符串 2 是",string2)

string3 = [[
"Lua 教程"
没有什么能够阻挡
我对自由的向往    
]]
print("字符串 3 是",string3)

astr = "OH! wdxtub.com"
print("字符串操作示例，原始字符串", astr)
print("全部转为大写", string.upper(astr))
print("全部转为小写", string.lower(astr))
print("替换字符串", string.gsub(astr, "OH", "WOW"))
print("查找字符串", string.find(astr, "wdxtub"))
print("字符串反转", string.reverse(astr))
print("字符串格式化输出", string.format("%s:%d", astr, 4))
print("将整型数字转成字符串并拼接", string.char(100,99,98,97))
print("将字符转成整型数值（默认第一个字符）", string.byte("abcd"), string.byte("abcd",4))
print("计算字符串长度", string.len(astr))
print("重复字符串", string.rep(astr, 2, "#"))
print("连接字符串.." .. astr)
print("匹配字符串")
for word in string.gmatch(astr, "%a+") do print(word) end
print("字符串截取", string.sub(astr, 5))

print("-------------")
print("一维数组")
array = {"Lua", "Tutorial"}
for i= 0, 2 do
   print(array[i])
end

print("多维数组")
-- 初始化数组
array = {}
for i=1,3 do
   array[i] = {}
      for j=1,3 do
         array[i][j] = i*j
      end
end

-- 访问数组
for i=1,3 do
   for j=1,3 do
      print(array[i][j])
   end
end

print("-------------")
print("迭代器")
array = {"wdxtub", "com"}

for key,value in ipairs(array)
do
   print(key, value)
end


print("-------------")
print("table")

-- 简单的 table
mytable = {}
print("mytable 的类型是 ",type(mytable))

mytable[1]= "Lua"
mytable["wow"] = "修改前"
print("mytable 索引为 1 的元素是 ", mytable[1])
print("mytable 索引为 wow 的元素是 ", mytable["wow"])

-- alternatetable和mytable的是指同一个 table
alternatetable = mytable

print("alternatetable 索引为 1 的元素是 ", alternatetable[1])
print("mytable 索引为 wow 的元素是 ", alternatetable["wow"])

alternatetable["wow"] = "修改后"

print("mytable 索引为 wow 的元素是 ", mytable["wow"])

-- 释放变量
alternatetable = nil
print("alternatetable 是 ", alternatetable)

-- mytable 仍然可以访问
print("mytable 索引为 wow 的元素是 ", mytable["wow"])

mytable = nil
print("mytable 是 ", mytable)

print("Table 函数")
fruits = {"banana","orange","apple"}
-- 返回 table 连接后的字符串
print("连接后的字符串 ",table.concat(fruits))
-- 指定连接字符
print("连接后的字符串 ",table.concat(fruits,", "))
-- 指定索引来连接 table
print("连接后的字符串 ",table.concat(fruits,", ", 2,3))
-- 在末尾插入
table.insert(fruits,"mango")
print("索引为 4 的元素为 ",fruits[4])
-- 在索引为 2 的键处插入
table.insert(fruits,2,"grapes")
print("索引为 2 的元素为 ",fruits[2])
print("最后一个元素为 ",fruits[5])
table.remove(fruits)
print("移除后最后一个元素为 ",fruits[5])
print("排序前")
for k,v in ipairs(fruits) do
        print(k,v)
end

table.sort(fruits)
print("排序后")
for k,v in ipairs(fruits) do
        print(k,v)
end